package com.mobibom.pipelines;

import java.util.Date;

import pipelines.free.beta.R;

import com.google.ads.AdRequest;
import com.google.ads.AdSize;
import com.google.ads.AdView;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Vibrator;
import android.view.Display;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

public class PipelinesActivity extends Activity {
    /** Called when the activity is first created. */
	final int HEIGHT = 12;
	final int WIDTH = 10;
	
    int[][] gradenhoek;//0 = 0 // 1 = 90 // 2 = 180 // 3 = 270   
    ImageView[][] grid = new ImageView[HEIGHT][WIDTH];
    //GROEN
  	//0 = niks // 1 = hoek // 2 = tstuk // 3 = begin //4 = eind//5 = streep//6=kruis
  	//BLAUW
  	//7=beginblauw//8=eindblauw//9=hoekblauw//10=streepblauw//11=tstukblauw//12=kruisblauw
    //COMBI
  	//13=kruiscombi // 14=hoekjecombi
    //DECORATIE
    //15=decoratie1 //16=decoratie2
    int[][] type;	
    boolean[][] geweestgroen;
    boolean[][] geweestblauw;
    int[][][] eigenuitgang;//0=links, 1=boven, 2=rechts, 3=onder//1=uitgang, 0=geen uitgang
	int[][] beginpuntengroen;
	int[][] beginpuntenblauw;
    int[][][] combiarray; // 0= leeg, 1 = groen, 2 = blauw , 3 = groenblauw
	
    int aantaleindgroen = 0; //Aantal eindpunten tbv winfunctie;
    int aantaleindblauw = 0;
    int aantalgroen = 0;
    int aantalblauw = 0;
    int aantalstappen = 0;
	long aantalseconden = 0;
	int aantalbeginpuntengroen =0;
	int aantalbeginpuntenblauw =0;
    String level;
    int levelpack;
	DatabaseHelper db;
	final int MAXLEVEL = 52;
	
	static final int DIALOG_COMPLETE_LEVEL = 1;
	static final int DIALOG_LEVELPACK_COMPLETE = 2;
	static final int POPUP_1 = 3;
	static final int POPUP_2 = 4;
	static final int POPUP_3 = 5;
	static final int POPUP_4 = 6;
	static final int POPUP_ALIEN_1 = 7;
	static final int POPUP_ALIEN_2 = 8;
	static final int POPUP_BIER_1 = 9;
	
	boolean vibrate;
	boolean backgroundgrid;
	
	Resources res;
	//advertentie
	private AdView adView1;
	private static final String MY_BANNER_UNIT_ID = "a14ed7bde839b79";
	 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        SharedPreferences settings = getSharedPreferences("levelsInDB", Context.MODE_PRIVATE);
	    
	    vibrate = settings.getBoolean("vibrate", true);
	    backgroundgrid = settings.getBoolean("grid", true);
        
        
        
        res = getResources();
        db = new DatabaseHelper(this);
    
        Bundle extras = getIntent().getExtras();
        if(extras !=null)
        {
        	levelpack = extras.getInt("levelpack");
        	level = extras.getString("level");
        	
        	setUpViews();
        	setClickListeners();
        	
        	String split[] = level.split( (levelpack+"x") );
        	
      	  	int nr = Integer.parseInt(split[1]);
        	
        	if(nr > MAXLEVEL){
        		//end level pack
        		showDialog(DIALOG_LEVELPACK_COMPLETE);
        	}
        	else{
        		if(levelpack == 1){
	        		if(level.equals("1x1")){
	        			showDialog(POPUP_1);
	        		}
	        		else if(level.equals("1x2")){
	        			showDialog(POPUP_2);
	        		}else if(level.equals("1x5")){
	        			showDialog(POPUP_3);
	        		}else if(level.equals("1x7")){
	        			showDialog(POPUP_4);
	        		}
        		}
        		else if(levelpack == 2){
        			if(level.equals("2x1")){
	        			showDialog(POPUP_ALIEN_1);
	        		}
	        		else if(level.equals("2x7")){
	        			showDialog(POPUP_ALIEN_2);
	        		}
        		}
        		else if(levelpack == 3){
        			if(level.equals("3x1")){
	        			showDialog(POPUP_BIER_1);
	        		}
        		}
        		setLevel(level);
        	}
        }
        else{
        	//geen level geselecteerd... terug naar elvel select?
        }
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK)) {
        	db.close();
        	Intent start = new Intent(getApplicationContext(), ChooseLevelActivity.class);
        	start.putExtra("levelpack", levelpack);
    		startActivity(start);
    		this.finish();
        }
        return super.onKeyDown(keyCode, event);
    }
    private void setLevel(String levelnr) {
    	
    	int[][] levelArray = new int[HEIGHT][WIDTH];
    	levelArray = db.getLevelArray(levelnr);
    	
    	aantalstappen = 0;
    	Date d = new Date();
        aantalseconden = d.getTime();
        
        gradenhoek = new int[HEIGHT][WIDTH];//0 = 0 // 1 = 90 // 2 = 180 // 3 = 270  
        type = new int[HEIGHT][WIDTH];
        geweestblauw = new boolean[HEIGHT][WIDTH];
        geweestgroen = new boolean[HEIGHT][WIDTH];
        eigenuitgang = new int[HEIGHT][WIDTH][4];//0=links, 1=boven, 2=rechts, 3=onder//1=uitgang, 0=geen uitgang
    	
        aantalbeginpuntengroen = 0;
        aantaleindgroen = 0;
        aantalgroen = 0;
        aantalstappen = 0;
        
        aantalbeginpuntenblauw = 0;
        aantaleindblauw = 0;
        aantalblauw = 0;
        
        s_kliks = "";
        s_seconden = "";
        
        gradenhoek = db.getLevelArrayRotate(levelnr);
        //GROEN
      	//0 = niks // 1 = hoek // 2 = tstuk // 3 = begin //4 = eind//5 = streep//6=kruis
      	//BLAUW
      	//7=beginblauw//8=eindblauw//9=hoekblauw//10=streepblauw//11=tstukblauw//12=kruisblauw
        //COMBI
      	//13=kruiscombi // 14=hoekjecombi
        //DECORATIE
        //15=decoratie1 //16=decoratie2
        for(int h = 0; h < HEIGHT; h++ ){
        	for(int w = 0; w < WIDTH; w++){
        		//0=niks
        		if(levelArray[h][w] == 0){
        			grid[h][w].setImageResource(R.drawable.geenpijp);
        			grid[h][w].setClickable(false);
        			type[h][w] = 0;
        		}
        		//1=hoek
        		else if(levelArray[h][w] == 1){
        			if(levelpack == 1){
        				grid[h][w].setImageResource(R.drawable.hoekje);
        			}else if(levelpack == 2){
        				grid[h][w].setImageResource(R.drawable.alien1hoekjeleeg);
        			}
        			else if(levelpack == 3){
        				grid[h][w].setImageResource(R.drawable.bierhoekje2);
        			}
        			type[h][w] = 1;
        			grid[h][w].setClickable(true);
        		}
        		//2=tstuk
        		else if(levelArray[h][w] == 2){
        			if(levelpack == 1){
        				grid[h][w].setImageResource(R.drawable.tstukje);
        			}else if(levelpack == 2){
        				grid[h][w].setImageResource(R.drawable.alien1tstukleeg);
        			}else if(levelpack == 3){
        				grid[h][w].setImageResource(R.drawable.biertstukje2);
        			}
        			
        			type[h][w] = 2;
        			grid[h][w].setClickable(true);
        		}
        		//3=begin
        		else if(levelArray[h][w] == 3){
        			if(levelpack == 1){
        				grid[h][w].setImageResource(R.drawable.begingroen);
        			}else if(levelpack == 2){
        				grid[h][w].setImageResource(R.drawable.alien1begin);
        			}else if(levelpack == 3){
        				grid[h][w].setImageResource(R.drawable.bierbegin);
        			}
        			
        			type[h][w] = 3;
        			aantalbeginpuntengroen++;
        			grid[h][w].setClickable(true);
        		}
        		//4=eind
        		else if(levelArray[h][w] == 4){
        			if(levelpack == 1){
        				grid[h][w].setImageResource(R.drawable.eindpunt);
        			}else if(levelpack == 2){
        				grid[h][w].setImageResource(R.drawable.alien1eindleeg);
        			}else if(levelpack == 3){
        				grid[h][w].setImageResource(R.drawable.biereindpunt);
        			}
        			
        			type[h][w] = 4;
        			aantaleindgroen++;
        			grid[h][w].setClickable(true);
        		}
        		//5=streep
        		else if(levelArray[h][w] == 5){
        			if(levelpack == 1){
        				grid[h][w].setImageResource(R.drawable.streep);
        			}else if(levelpack == 2){
        				grid[h][w].setImageResource(R.drawable.alien1streepleeg);
        			}else if(levelpack == 3){
        				grid[h][w].setImageResource(R.drawable.bierstreep2);
        			}
        			type[h][w] = 5;
        			grid[h][w].setClickable(true);
        		}
        		//6=kruis
        		else if(levelArray[h][w] == 6){
        			if(levelpack == 1){
        				grid[h][w].setImageResource(R.drawable.kruis);
        			}else if(levelpack == 2){
        				grid[h][w].setImageResource(R.drawable.alien1kruisleeg);
        			}else if(levelpack == 3){
        				grid[h][w].setImageResource(R.drawable.bierkruis2);
        			}
        			type[h][w] = 6;
        			grid[h][w].setClickable(true);
        		}
        		//7=beginblauw
        		else if(levelArray[h][w] == 7){
        			if(levelpack == 1){
        				grid[h][w].setImageResource(R.drawable.begin2blauw);
        			}else if(levelpack == 2){
        				grid[h][w].setImageResource(R.drawable.alien2begin);
        			}
        			type[h][w] = 7;
        			aantalbeginpuntenblauw++;
        			grid[h][w].setClickable(true);
        		}
        		//8=eindblauw
        		else if(levelArray[h][w] == 8){
        			if(levelpack == 1){
        				grid[h][w].setImageResource(R.drawable.eindpunt2);
        			}else if(levelpack == 2){
        				grid[h][w].setImageResource(R.drawable.alien2eindleeg);
        			}
        			type[h][w] = 8;
        			aantaleindblauw++;
        			grid[h][w].setClickable(true);
        		}
        		//9=hoekblauw
        		else if(levelArray[h][w] == 9){
           			if(levelpack == 1){
        				grid[h][w].setImageResource(R.drawable.hoekje2);
        			}else if(levelpack == 2){
        				grid[h][w].setImageResource(R.drawable.alien2hoekjeleeg);
        			}
        			type[h][w] = 9;
        			grid[h][w].setClickable(true);
        		}
        		//10=streepblauw
        		else if(levelArray[h][w] == 10){
        			if(levelpack == 1){
        				grid[h][w].setImageResource(R.drawable.streep2);
        			}else if(levelpack == 2){
        				grid[h][w].setImageResource(R.drawable.alien2streepleeg);
        			}
        			
        			type[h][w] = 10;
        			grid[h][w].setClickable(true);
        		}
        		//11=tstukblauw
        		else if(levelArray[h][w] == 11){
        			if(levelpack == 1){
        				grid[h][w].setImageResource(R.drawable.tstukje2);
        			}else if(levelpack == 2){
        				grid[h][w].setImageResource(R.drawable.alien2tstukleeg);
        			}
        			type[h][w] = 11;
        			grid[h][w].setClickable(true);
        		}
        		//12=kruisblauw
        		else if(levelArray[h][w] == 12){
        			if(levelpack == 1){
        				grid[h][w].setImageResource(R.drawable.kruis2);
        			}else if(levelpack == 2){
        				grid[h][w].setImageResource(R.drawable.alien2kruisleeg);
        			}
        			
        			type[h][w] = 12;
        			grid[h][w].setClickable(true);
        		}//13 kruiscombi
        		else if(levelArray[h][w] == 13){
        			if(levelpack == 1){
        				grid[h][w].setImageResource(R.drawable.kruiscombi);
        			}else if(levelpack == 2){
        				grid[h][w].setImageResource(R.drawable.alienkruiscombileeg);
        			}
        			type[h][w] = 13;
        			grid[h][w].setClickable(true);
        		}//14 hoekjecombi
        		else if(levelArray[h][w] == 14){
        			if(levelpack == 1){
        				grid[h][w].setImageResource(R.drawable.hoekjecombi);
        			}else if(levelpack == 2){
        				grid[h][w].setImageResource(R.drawable.alienhoekjecombileeg);
        			}
        			type[h][w] = 14;
        			grid[h][w].setClickable(true);
        		}//15 decoratie1
        		else if(levelArray[h][w] == 15){
        			if(levelpack == 1){
        				grid[h][w].setImageResource(R.drawable.decoratie1);
        			}else if(levelpack == 2){
        				grid[h][w].setImageResource(R.drawable.aliendecoratie1);
        			}
        			type[h][w] = 15;
        			grid[h][w].setClickable(false);
        			gradenhoek[h][w] = 0;
        		}//15 decoratie2
        		else if(levelArray[h][w] == 16){
        			if(levelpack == 1){
        				grid[h][w].setImageResource(R.drawable.decoratie2);
        			}else if(levelpack == 2){
        				grid[h][w].setImageResource(R.drawable.aliendecoratie2);
        			}
            		type[h][w] = 16;
            		grid[h][w].setClickable(false);
            		gradenhoek[h][w] = 0;
            	}
        	}
        }
        
        combiarray = new int[HEIGHT][WIDTH][1];
        
    	beginpuntengroen = new int[aantalbeginpuntengroen][2];
    	beginpuntenblauw = new int[aantalbeginpuntenblauw][2];
    	
    	int beginpuntcountgroen = 0;
    	int beginpuntcountblauw = 0;
    	for(int h = 0; h < HEIGHT; h++){
    		for(int w = 0; w < WIDTH; w++){
    			if(type[h][w] == 3){
    				beginpuntengroen[beginpuntcountgroen][0] = h;
    				beginpuntengroen[beginpuntcountgroen][1] = w;
    				beginpuntcountgroen++;
    			}else if(type[h][w] == 7){
    				beginpuntenblauw[beginpuntcountblauw][0] = h;
    				beginpuntenblauw[beginpuntcountblauw][1] = w;
    				beginpuntcountblauw++;
    			}
    			else if(type[h][w] == 13 || type[h][w] == 14){
    				combiarray[h][w][0] = 0;
    			}
    			setValuesOfPipe(h,w);
    			rotateGreenPipe(h, w, gradenhoek[h][w]);
    		}
    	}
    	resetGeweest();
    	//check level direct
     	aantalgroen = 0;
     	aantalblauw = 0;
     	
     	for(int i=0; i< aantalbeginpuntengroen;i++){
     		checkFromStart(beginpuntengroen[i][0],beginpuntengroen[i][1],true,"groen");
     	}
       	for(int i=0; i< aantalbeginpuntenblauw;i++){
     		checkFromStart(beginpuntenblauw[i][0],beginpuntenblauw[i][1],true,"blauw");
     	}
    	
	}

	private void resetGeweest(){
    	for(int h = 0; h < HEIGHT; h++){
    		for(int w = 0; w < WIDTH; w++){
    			geweestblauw[h][w] = false;
    			geweestgroen[h][w] = false;
    			combiarray[h][w][0]=0;
    		}
    	}
    }
    
    private void checkFromStart(int h,int w, boolean x, String kleur){
    	boolean y;
    	if(x){
    		y = true;
    	}else{
    		y = false;
    	}
    	if(kleur.equals("groen")){
    		geweestgroen[h][w] = true;
    	}
    	else if(kleur.equals("blauw")){
    		geweestblauw[h][w] = true;
    	}
    	//GROEN
    	//0 = niks // 1 = hoek // 2 = tstuk // 3 = begin //4 = eind//5 = streep//6=kruis
    	 if ( type[h][w] == 5 )
    	 {
    		 if(levelpack == 1){
    			 grid[h][w].setImageResource(R.drawable.streepgroen);
    		 }else if(levelpack == 2){
    			 grid[h][w].setImageResource(R.drawable.alien1streepvol);
    		 }else if(levelpack == 3){
    			 grid[h][w].setImageResource(R.drawable.bierstreep);
    		 }
             rotateGreenPipe(h,w,gradenhoek[h][w]);
    	 }
         else if ( type[h][w] == 1 ){
        	 if(levelpack == 1){
    			 grid[h][w].setImageResource(R.drawable.hoekgroen);
    		 }else if(levelpack == 2){
    			 grid[h][w].setImageResource(R.drawable.alien1hoekvol);
    		 }else if(levelpack == 3){
    			 grid[h][w].setImageResource(R.drawable.bierhoekje);
    		 }
        	 rotateGreenPipe(h,w,gradenhoek[h][w]);
         }
         else if ( type[h][w] == 2 ){
        	 if(levelpack == 1){
    			 grid[h][w].setImageResource(R.drawable.tstukgroen);
    		 }else if(levelpack == 2){
    			 grid[h][w].setImageResource(R.drawable.alien1tstukvol);
    		 }else if(levelpack == 3){
    			 grid[h][w].setImageResource(R.drawable.biertstukje);
    		 }
        	 rotateGreenPipe(h,w,gradenhoek[h][w]);
         }
         else if ( type[h][w] == 4 )
         { 
        	 if(levelpack == 1){
    			 grid[h][w].setImageResource(R.drawable.eindpuntgroen);
    		 }else if(levelpack == 2){
    			 grid[h][w].setImageResource(R.drawable.alien1eindvol);
    		 }else if(levelpack == 3){
    			 grid[h][w].setImageResource(R.drawable.biereindpuntvol);
    		 }
        	 
        	 rotateGreenPipe(h,w,gradenhoek[h][w]);
        	 if(y)
        	 {
        		 aantalgroen++;
        		 if (aantaleindgroen == aantalgroen)
                 {
        			 if( checkAllStartPipesAreConnected() )
        			 {
        				 win();
        			 }
                 }
        	 }
         }
         else if( type[h][w] == 6){
        	 if(levelpack == 1){
    			 grid[h][w].setImageResource(R.drawable.kruisgroen);
    		 }else if(levelpack == 2){
    			 grid[h][w].setImageResource(R.drawable.alien1kruisvol);
    		 }else if(levelpack == 3){
    			 grid[h][w].setImageResource(R.drawable.bierkruis);
    		 }
        	 rotateGreenPipe(h, w, gradenhoek[h][w]);
         }
    	//BLAUW
     	//7=beginblauw//8=eindblauw//9=hoekblauw//10=streepblauw//11=tstukblauw//12=kruisblauw
         else if ( type[h][w] == 10 )
    	 {
        		if(levelpack == 1){
    				grid[h][w].setImageResource(R.drawable.streep2blauw);
    			}else if(levelpack == 2){
    				grid[h][w].setImageResource(R.drawable.alien2streepvol);
    			}
             rotateGreenPipe(h,w,gradenhoek[h][w]);
    	 }
         else if ( type[h][w] == 9 ){
        		if(levelpack == 1){
    				 grid[h][w].setImageResource(R.drawable.hoek2blauw);
    			}else if(levelpack == 2){
    				grid[h][w].setImageResource(R.drawable.alien2hoekjevol);
    			}
        	 rotateGreenPipe(h,w,gradenhoek[h][w]);
         }
         else if ( type[h][w] == 11 ){
        		if(levelpack == 1){
    				grid[h][w].setImageResource(R.drawable.tstuk2blauw);
    			}else if(levelpack == 2){
    				grid[h][w].setImageResource(R.drawable.alien2tstukvol);
    			}
        	 rotateGreenPipe(h,w,gradenhoek[h][w]);
         }
         else if ( type[h][w] == 8 )
         { 
        		if(levelpack == 1){
    				grid[h][w].setImageResource(R.drawable.eindpunt2blauw);
    			}else if(levelpack == 2){
    				grid[h][w].setImageResource(R.drawable.alien2eindvol);
    			}
        	 
        	 rotateGreenPipe(h,w,gradenhoek[h][w]);
        	 if(y)
        	 {
        		 aantalblauw++;
        		 if (aantaleindblauw == aantalblauw)
                 {
        			 if( checkAllStartPipesAreConnected() )
        			 {
        				 win();
        			 }
                 }
        	 }
         }
         else if( type[h][w] == 12){
        		if(levelpack == 1){
    				 grid[h][w].setImageResource(R.drawable.kruis2blauw);
    			}else if(levelpack == 2){
    				grid[h][w].setImageResource(R.drawable.alien2kruisvol);
    			}
        	 rotateGreenPipe(h, w, gradenhoek[h][w]);
         }
    	 //13 = kruiscombi 
         else if( type[h][w] == 13){
        	 if(kleur.equals("groen")){
        		 if(combiarray[h][w][0] == 0){
        				if(levelpack == 1){
            				 grid[h][w].setImageResource(R.drawable.kruiscombigroen);
            			}else if(levelpack == 2){
            				grid[h][w].setImageResource(R.drawable.alienkruiscombi1);
            			}
        			
        			 combiarray[h][w][0] = 1;
        		 }
        		 else if(combiarray[h][w][0] == 1){
        				if(levelpack == 1){
            				 grid[h][w].setImageResource(R.drawable.kruiscombigroen);
            			}else if(levelpack == 2){
            				grid[h][w].setImageResource(R.drawable.alienkruiscombi1);
            			}
        			 combiarray[h][w][0] = 1;
        		 }
        		 else if(combiarray[h][w][0] == 2){
        				if(levelpack == 1){
            				grid[h][w].setImageResource(R.drawable.kruiscombigroenblauw);
            			}else if(levelpack == 2){
            				grid[h][w].setImageResource(R.drawable.alienkruiscombivol);
            			}
        			
        			 combiarray[h][w][0] = 3;
        		 }
        		 else if(combiarray[h][w][0] == 3){
        				if(levelpack == 1){
            				grid[h][w].setImageResource(R.drawable.kruiscombigroenblauw);
            			}else if(levelpack == 2){
            				grid[h][w].setImageResource(R.drawable.alienkruiscombivol);
            			}
        			 combiarray[h][w][0] = 3;
        		 }
        		 
        	}
        	 else if(kleur.equals("blauw")){
	    		 if(combiarray[h][w][0] == 0){
	    				if(levelpack == 1){
	        				grid[h][w].setImageResource(R.drawable.kruiscombiblauw);
	        			}else if(levelpack == 2){
	        				grid[h][w].setImageResource(R.drawable.alienkruiscombi2);
	        			}
	    			 combiarray[h][w][0] = 2;
	    		 }
	    		 else if(combiarray[h][w][0] == 1){
	    				if(levelpack == 1){
	        				grid[h][w].setImageResource(R.drawable.kruiscombigroenblauw);
	        			}else if(levelpack == 2){
	        				grid[h][w].setImageResource(R.drawable.alienkruiscombivol);
	        			}
	    			 combiarray[h][w][0] = 3;
	    		 }
	    		 else if(combiarray[h][w][0] == 2){
	    				if(levelpack == 1){
	        				grid[h][w].setImageResource(R.drawable.kruiscombigroenblauw);
	        			}else if(levelpack == 2){
	        				grid[h][w].setImageResource(R.drawable.alienkruiscombivol);
	        			}
	    			 
	    			 combiarray[h][w][0] = 2;
	    		 }
	    		 else if(combiarray[h][w][0] == 3){
	    				if(levelpack == 1){
	        				grid[h][w].setImageResource(R.drawable.kruiscombigroenblauw);
	        			}else if(levelpack == 2){
	        				grid[h][w].setImageResource(R.drawable.alienkruiscombivol);
	        			}
	    			 combiarray[h][w][0] = 3;
	    		 }
        	 }
        	 rotateGreenPipe(h,w,gradenhoek[h][w]);
         }
    	 // 14= hoekjecombi
         else if( type[h][w] == 14){
        	 if(kleur.equals("groen")){
        		 if(combiarray[h][w][0] == 0){
        				if(levelpack == 1){
            				grid[h][w].setImageResource(R.drawable.hoekjecombigroen);
            			}else if(levelpack == 2){
            				grid[h][w].setImageResource(R.drawable.alienhoekjecombi1);
            			}
        			 
        			 combiarray[h][w][0] = 1;
        		 }
        		 else if(combiarray[h][w][0] == 1){
        				if(levelpack == 1){
            				grid[h][w].setImageResource(R.drawable.hoekjecombigroen);
            			}else if(levelpack == 2){
            				grid[h][w].setImageResource(R.drawable.alienhoekjecombi1);
            			}
        			 combiarray[h][w][0] = 1;
        		 }
        		 else if(combiarray[h][w][0] == 2){
        				if(levelpack == 1){
            				grid[h][w].setImageResource(R.drawable.hoekjecombigroenblauw);
            			}else if(levelpack == 2){
            				grid[h][w].setImageResource(R.drawable.alienhoekjecombivol);
            			}
        			 combiarray[h][w][0] = 3;
        		 }
        		 else if(combiarray[h][w][0] == 3){
        				if(levelpack == 1){
            				grid[h][w].setImageResource(R.drawable.hoekjecombigroenblauw);
            			}else if(levelpack == 2){
            				grid[h][w].setImageResource(R.drawable.alienhoekjecombivol);
            			}
        			 combiarray[h][w][0] = 3;
        		 }
        		 
        	}
        	 else if(kleur.equals("blauw")){
	    		 if(combiarray[h][w][0] == 0){
	    				if(levelpack == 1){
            				grid[h][w].setImageResource(R.drawable.hoekjecombiblauw);
            			}else if(levelpack == 2){
            				grid[h][w].setImageResource(R.drawable.alienhoekjecombi2);
            			}
	    			 combiarray[h][w][0] = 2;
	    		 }
	    		 else if(combiarray[h][w][0] == 1){
	    				if(levelpack == 1){
            				grid[h][w].setImageResource(R.drawable.hoekjecombigroenblauw);
            			}else if(levelpack == 2){
            				grid[h][w].setImageResource(R.drawable.alienhoekjecombivol);
            			}
	    			 combiarray[h][w][0] = 3;
	    		 }
	    		 else if(combiarray[h][w][0] == 2){
	    				if(levelpack == 1){
            				grid[h][w].setImageResource(R.drawable.hoekjecombigroenblauw);
            			}else if(levelpack == 2){
            				grid[h][w].setImageResource(R.drawable.alienhoekjecombivol);
            			}
	    			 combiarray[h][w][0] = 2;
	    		 }
	    		 else if(combiarray[h][w][0] == 3){
	    				if(levelpack == 1){
            				grid[h][w].setImageResource(R.drawable.hoekjecombigroenblauw);
            			}else if(levelpack == 2){
            				grid[h][w].setImageResource(R.drawable.alienhoekjecombivol);
            			}
	    			 combiarray[h][w][0] = 3;
	    		 }
        	 }
        	 rotateGreenPipe(h,w,gradenhoek[h][w]);
         }	 
        	 
    	 if(kleur.equals("groen")){
	    	 if (eigenuitgang[h][w][0] == 1 && eigenuitgang[h][(w - 1)][2] == 1 && !geweestgroen[h][(w-1)])
	    		 checkFromStart(h, (w - 1), y, "groen");
	         if (eigenuitgang[h][w][1] == 1 && eigenuitgang[(h - 1)][w][3] == 1 && !geweestgroen[(h-1)][w])
	        	 checkFromStart( (h-1), w, y, "groen");
	         if (eigenuitgang[h][w][2] == 1 && eigenuitgang[h][(w + 1)][0] == 1 && !geweestgroen[h][(w + 1)])
	        	 checkFromStart(h, (w + 1) ,y, "groen");
	         if (eigenuitgang[h][w][3] == 1 && eigenuitgang[(h + 1)][w][1] == 1 && !geweestgroen[(h+1)][w])
	        	 checkFromStart( (h+1), w, y, "groen");
    	 }
         //blauw
    	 if(kleur.equals("blauw")){
	         if (eigenuitgang[h][w][0] == 2 && eigenuitgang[h][(w - 1)][2] == 2 && !geweestblauw[h][(w-1)])
	    		 checkFromStart(h, (w - 1), y,"blauw");
	         if (eigenuitgang[h][w][1] == 2 && eigenuitgang[(h - 1)][w][3] == 2 && !geweestblauw[(h-1)][w])
	        	 checkFromStart( (h-1), w, y,"blauw");
	         if (eigenuitgang[h][w][2] == 2 && eigenuitgang[h][(w + 1)][0] == 2 && !geweestblauw[h][(w + 1)])
	        	 checkFromStart(h, (w + 1) ,y, "blauw");
	         if (eigenuitgang[h][w][3] == 2 && eigenuitgang[(h + 1)][w][1] == 2 && !geweestblauw[(h+1)][w])
	        	 checkFromStart( (h+1), w, y, "blauw");
    	 }
    }

    private boolean checkAllStartPipesAreConnected() {
		boolean startPipesConnected = true;
    	
		for(int i= 0; i < aantalbeginpuntengroen; i++){
    		int h = beginpuntengroen[i][0];
    		int w = beginpuntengroen[i][1];
    		
    		for(int j = 0; j <4;j++){
    			if( eigenuitgang[h][w][j] == 1 ){
    				if( j == 0 )//check links
    				{
    					if(eigenuitgang[h][(w-1)][2] != 1)
    					{
    						startPipesConnected = false;
    						break;
    					}
    				}
    				else if( j == 1)//boven
    				{
    					if(eigenuitgang[(h-1)][(w)][3] != 1)
    					{
    						startPipesConnected = false;
    						break;
    					}
    				}
    				else if( j == 2)//rechts
    				{
    					if(eigenuitgang[(h)][(w+1)][0] != 1)
    					{
    						startPipesConnected = false;
    						break;
    					}
    				}
    				else if( j == 3)//onder
    				{
    					if(eigenuitgang[(h+1)][(w)][1] != 1)
    					{
    						startPipesConnected = false;
    						break;
    					}
    				}
    			}
    		}
    	}
    		for(int i2= 0; i2 < aantalbeginpuntenblauw; i2++){
        		int h2 = beginpuntenblauw[i2][0];
        		int w2 = beginpuntenblauw[i2][1];
        		for(int j2 = 0; j2 <4;j2++){
    			//check blauwe buizen
    			if(eigenuitgang[h2][w2][j2] == 2){
    				if( j2 == 0 )//check links
    				{
    					if(eigenuitgang[h2][(w2-1)][2] != 2)
    					{
    						startPipesConnected = false;
    						break;
    					}
    				}
    				else if( j2 == 1)//boven
    				{
    					if(eigenuitgang[(h2-1)][(w2)][3] != 2)
    					{
    						startPipesConnected = false;
    						break;
    					}
    				}
    				else if( j2 == 2)//rechts
    				{
    					if(eigenuitgang[(h2)][(w2+1)][0] != 2)
    					{
    						startPipesConnected = false;
    						break;
    					}
    				}
    				else if( j2 == 3)//onder
    				{
    					if(eigenuitgang[(h2+1)][(w2)][1] != 2)
    					{
    						startPipesConnected = false;
    						break;
    					}
    				}
    			}
    		}
    	}
    	if( startPipesConnected ){
    		for(int h = 0; h < HEIGHT; h++){
        		for(int w = 0; w < WIDTH; w++){
			    	 if (eigenuitgang[h][w][0] == 1 && eigenuitgang[h][(w - 1)][2] == 0)
			    		 startPipesConnected = false;
			         if (eigenuitgang[h][w][1] == 1 && eigenuitgang[(h - 1)][w][3] == 0)
			        	 startPipesConnected = false;
			         if (eigenuitgang[h][w][2] == 1 && eigenuitgang[h][(w + 1)][0] == 0)
			        	 startPipesConnected = false;
			         if (eigenuitgang[h][w][3] == 1 && eigenuitgang[(h + 1)][w][1] == 0)
			        	 startPipesConnected = false;
			         
			    	 if (eigenuitgang[h][w][0] == 2 && eigenuitgang[h][(w - 1)][2] == 0)
			    		 startPipesConnected = false;
			         if (eigenuitgang[h][w][1] == 2 && eigenuitgang[(h - 1)][w][3] == 0)
			        	 startPipesConnected = false;
			         if (eigenuitgang[h][w][2] == 2 && eigenuitgang[h][(w + 1)][0] == 0)
			        	 startPipesConnected = false;
			         if (eigenuitgang[h][w][3] == 2 && eigenuitgang[(h + 1)][w][1] == 0)
			        	 startPipesConnected = false;
			    	 if (eigenuitgang[h][w][0] == 2 && eigenuitgang[h][(w - 1)][2] == 1)
			    		 startPipesConnected = false;
			         if (eigenuitgang[h][w][1] == 2 && eigenuitgang[(h - 1)][w][3] == 1)
			        	 startPipesConnected = false;
			         if (eigenuitgang[h][w][2] == 2 && eigenuitgang[h][(w + 1)][0] == 1)
			        	 startPipesConnected = false;
			         if (eigenuitgang[h][w][3] == 2 && eigenuitgang[(h + 1)][w][1] == 1)
			        	 startPipesConnected = false;
        		}
        	}
    	}
		return startPipesConnected;
	}

	private void rotateGreenPipe(int h,int w, int rotate ){
    	
    	Bitmap bmp = ((BitmapDrawable)grid[h][w].getDrawable()).getBitmap();
      	// Getting width & height of the given image.
      	int width = bmp.getWidth();
      	int height = bmp.getHeight();
      	// Setting post rotate to 90
      	Matrix mtx = new Matrix();
      	mtx.postRotate(rotate);
      	// Rotating Bitmap
      	Bitmap rotatedBMP = Bitmap.createBitmap(bmp, 0, 0, width, height, mtx, true);
      	BitmapDrawable bmd = new BitmapDrawable(rotatedBMP);
      	grid[h][w].setImageDrawable(bmd);
    }
	
	private void win() {
		SharedPreferences settings = getSharedPreferences("levelsInDB", Context.MODE_PRIVATE);
		//level complete
    	if(levelpack == 1){
    		String[] l = level.split("1x");
    		if( (Integer.parseInt(l[1])+1) > settings.getInt("wereld1level", 0) &&  (Integer.parseInt(l[1])+1) <= MAXLEVEL ){
            	Editor editor = settings.edit();
            	editor.putInt("wereld1level", (Integer.parseInt(l[1])+1)  );
            	editor.commit();
    		}
    	}
    	else if(levelpack == 2){
    		String[] l = level.split("2x");
    		if( (Integer.parseInt(l[1])+1) > settings.getInt("wereld2level", 0) &&  (Integer.parseInt(l[1])+1) <= MAXLEVEL ){
            	Editor editor = settings.edit();
            	editor.putInt("wereld2level", (Integer.parseInt(l[1])+1)  );
            	editor.commit();
    		}
    	}
    	else if(levelpack == 3){
    		String[] l = level.split("3x");
    		if( (Integer.parseInt(l[1])+1) > settings.getInt("wereld3level", 0) &&  (Integer.parseInt(l[1])+1) <= MAXLEVEL ){
            	Editor editor = settings.edit();
            	editor.putInt("wereld3level", (Integer.parseInt(l[1])+1)  );
            	editor.commit();
    		}
    	}	
    		
    	Date d = new Date();
		//chekc topscores
		int topscoreSec = db.getTopscoreSecLevel(level);
    	int topscoreKliks = db.getTopscoreKliksLevel(level);
    	
    	s_seconden = "";
    	s_kliks = "";
    	aantalsec = ( (d.getTime() - aantalseconden) /1000);
    	// nieuwe topscore seconden
    	if(topscoreSec == 0 || topscoreSec > aantalsec){
    		s_seconden = res.getString(R.string.LevelCompletedTopscoreSeconds) + " " + aantalsec;
    		db.updateTopScoreSec(level, (int)aantalsec);
    	}
    	//geen topscore seconden
    	else{
    		s_seconden = res.getString(R.string.LevelCompletedNoTopscoreSeconds) + " " + aantalsec;
    	}
    	// nieuwe topscore kliks
    	if(topscoreKliks == 0 || topscoreKliks > aantalstappen){
    		s_kliks = res.getString(R.string.LevelCompletedTopscoreClicks) + " " + aantalstappen;
    		db.updateTopScoreKliks(level, aantalstappen);
    	}
    	//geen topscore kliks
    	else{
    		s_kliks = res.getString(R.string.LevelCompletedNoTopscoreClicks) + " " + aantalstappen;
    	}
    	showDialog(DIALOG_COMPLETE_LEVEL);
    	
    	
	}
	String s_seconden;
	String s_kliks;
	long aantalsec;
	
	private int checkSterren(long sec ){
		int aantalSterren = 0;
		
		if(levelpack == 1){
			// 3 sterren
			if(sec == 0){
				aantalSterren = 0;
			}
			else  if(sec < 30 ){
				aantalSterren = 3;
			}
			// 2 sterren
			else if(sec < 60){
				aantalSterren = 2;
			}
			// 1 sterren
			else if(sec < 90){
				aantalSterren = 1;
			}
			// 0 sterren
			else{
				aantalSterren = 0;
			}
		}
		//alien
		else if(levelpack == 2){
			// 3 sterren
			if(sec == 0){
				aantalSterren = 0;
			}
			else if(sec < 60 ){
				aantalSterren = 3;
			}
			// 2 sterren
			else if(sec < 90){
				aantalSterren = 2;
			}
			// 1 sterren
			else if(sec < 120){
				aantalSterren = 1;
			}
			// 0 sterren
			else{
				aantalSterren = 0;
			}
		}//bier
		else if(levelpack == 3){
			if(sec == 0){
				aantalSterren = 0;
			}
			// 3 sterren
			else if(sec < 70 ){
				aantalSterren = 3;
			}
			// 2 sterren
			else if(sec < 100){
				aantalSterren = 2;
			}
			// 1 sterren
			else if(sec < 130){
				aantalSterren = 1;
			}
			// 0 sterren
			else{
				aantalSterren = 0;
			}
		}
			
		return aantalSterren;
	}
	
	
	@Override
    public Dialog onCreateDialog(int id) {	
		switch (id) {
		case DIALOG_COMPLETE_LEVEL:
      	  String[] split = level.split(levelpack+"x");
      	  int levelnr = (Integer.parseInt(split[1]));
      	  //int aantalSec = Integer.parseInt(s_seconden);
      	  int aantalSterren = checkSterren(aantalsec); 
      	  
      	final ImageView imgSterren = new ImageView(this);
      	//western
      		if(levelpack == 1){
      			// 3 sterren
      			if(aantalSterren == 3 ){
      				imgSterren.setImageResource(R.drawable.western3sterren);
      			}
      			// 2 sterren
      			else if(aantalSterren == 2){
      				imgSterren.setImageResource(R.drawable.western2sterren);
      			}
      			// 1 sterren
      			else if(aantalSterren == 1){
      				imgSterren.setImageResource(R.drawable.western1sterren);
      			}
      			// 0 sterren
      			else{
      				imgSterren.setImageResource(R.drawable.western0sterren);
      			}
      		}
      		//alien
      		else if(levelpack == 2){// TODO
      			// 3 sterren
      			if(aantalSterren == 3 ){
      				imgSterren.setImageResource(R.drawable.alien3sterren);
      			}
      			// 2 sterren
      			else if(aantalSterren == 2){
      				imgSterren.setImageResource(R.drawable.alien2sterren);
      			}
      			// 1 sterren
      			else if(aantalSterren == 1){
      				imgSterren.setImageResource(R.drawable.alien1sterren);
      			}
      			// 0 sterren
      			else{
      				imgSterren.setImageResource(R.drawable.alien0sterren);
      			}
      		}
      		else if(levelpack == 3){// TODO
      			// 3 sterren
      			if(aantalSterren == 3 ){
      				imgSterren.setImageResource(R.drawable.bier3);
      			}
      			// 2 sterren
      			else if(aantalSterren == 2){
      				imgSterren.setImageResource(R.drawable.bier2);
      			}
      			// 1 sterren
      			else if(aantalSterren == 1){
      				imgSterren.setImageResource(R.drawable.bier1);
      			}
      			// 0 sterren
      			else{
      				imgSterren.setImageResource(R.drawable.bier0);
      			}
      		}
		
      	  imgSterren.setPadding(10, 10, 10, 10);
      	  
			 return new AlertDialog.Builder(PipelinesActivity.this)
	          .setTitle(res.getString(R.string.LevelCompletedTitle) + " " + levelnr + " " + res.getString(R.string.LevelCompletedTitle2)  )
	          .setMessage(	/*s_kliks + "\n" +*/
	          				s_seconden +"\n" )
	          .setView(imgSterren)
	          .setPositiveButton(res.getString(R.string.buttonRestartLevel), new DialogInterface.OnClickListener() {
	              public void onClick(DialogInterface dialog, int whichButton) {
	            	 setLevel(level);
	            	 removeDialog(DIALOG_COMPLETE_LEVEL);
	            	 
	              }
	          })
	         .setNeutralButton(res.getString(R.string.buttonBackLevelChose), new DialogInterface.OnClickListener() {
	              public void onClick(DialogInterface dialog, int whichButton) {
	            	    Intent choseLevel = new Intent(getApplicationContext(), ChooseLevelActivity.class);
	            	    db.close();
		          		choseLevel.putExtra("levelpack", levelpack);
		          		startActivity(choseLevel);
		          		finish();
	              }
	          })
	          .setNegativeButton(res.getString(R.string.buttonNextLevel), new DialogInterface.OnClickListener() {
	              public void onClick(DialogInterface dialog, int whichButton) {
	            	  String[] split = level.split(levelpack+"x");
	            	  int nr = (Integer.parseInt(split[1])+1);
		              String s = levelpack + "x" + nr;
		              db.close();
	            	  Intent level = new Intent(getApplicationContext(), PipelinesActivity.class);
		          	  level.putExtra("level", s);
		          	  level.putExtra("levelpack", levelpack);
		              startActivity(level);
		              finish();
	              }
	          })
			 .create();
			case DIALOG_LEVELPACK_COMPLETE:
			 return new AlertDialog.Builder(PipelinesActivity.this)
	          .setTitle(res.getString(R.string.LevelpackCompletedTitle))
	          .setMessage(res.getString(R.string.LevelpackCompletedMessage))
	          .setPositiveButton("Oke", new DialogInterface.OnClickListener() {
	              public void onClick(DialogInterface dialog, int whichButton) {
		            	Intent choseLevel = new Intent(getApplicationContext(), LevelPackActivity.class);
	            	  	choseLevel.putExtra("levelpack", levelpack);
	            	  	db.close();
		          		startActivity(choseLevel);
		          		finish();
	              }
	          })
	          .create();
				case POPUP_1:
				final ImageView img = new ImageView(this);
				img.setImageResource(R.drawable.howtoplay_image1);
				 return new AlertDialog.Builder(PipelinesActivity.this)
		          .setTitle("How to play?! - 1")
	              .setView(img)
		          .setMessage(res.getString(R.string.howtoplayPopUp1))
		          .setPositiveButton("Oke", new DialogInterface.OnClickListener() {
		              public void onClick(DialogInterface dialog, int whichButton) {
			            	
		              }
		          })
		          .create();
				case POPUP_2:
				 return new AlertDialog.Builder(PipelinesActivity.this)
		          .setTitle("How to play?! - 2")
		          .setMessage(res.getString(R.string.howtoplayPopUp2))
		          .setPositiveButton("Oke", new DialogInterface.OnClickListener() {
		              public void onClick(DialogInterface dialog, int whichButton) {
			            	
		              }
		          })
		          .create();
				case POPUP_3:
				final ImageView img2 = new ImageView(this);
				img2.setImageResource(R.drawable.howtoplay_image3);
				 return new AlertDialog.Builder(PipelinesActivity.this)
		          .setTitle("How to play?! - 3")
		          .setView(img2)
		          .setMessage(res.getString(R.string.howtoplayPopUp3))
		          .setPositiveButton("Oke", new DialogInterface.OnClickListener() {
		              public void onClick(DialogInterface dialog, int whichButton) {
			            	
		              }
		          })
		          .create();
				case POPUP_4:
				 return new AlertDialog.Builder(PipelinesActivity.this)
		          .setTitle("How to play?! - 4")
		          .setMessage(res.getString(R.string.howtoplayPopUp4))
		          .setPositiveButton("Oke", new DialogInterface.OnClickListener() {
		              public void onClick(DialogInterface dialog, int whichButton) {
			            	
		              }
		          })
		          .create();
				case POPUP_ALIEN_1:
					final ImageView img3 = new ImageView(this);
					img3.setImageResource(R.drawable.howtoplay_alien_image1);
					 return new AlertDialog.Builder(PipelinesActivity.this)
			          .setTitle("How to play?! - 5")
		              .setView(img3)
			          .setMessage(res.getString(R.string.howtoplayPopUpAlien1))
			          .setPositiveButton("Oke", new DialogInterface.OnClickListener() {
			              public void onClick(DialogInterface dialog, int whichButton) {
				            	
			              }
			          })
			          .create();
				case POPUP_ALIEN_2:
					final ImageView img4 = new ImageView(this);
					img4.setImageResource(R.drawable.howtoplay_alien_image2); //TODO
					 return new AlertDialog.Builder(PipelinesActivity.this)
			          .setTitle("How to play?! - 6")
		              .setView(img4)
			          .setMessage(res.getString(R.string.howtoplayPopUpAlien2))
			          .setPositiveButton("Oke", new DialogInterface.OnClickListener() {
			              public void onClick(DialogInterface dialog, int whichButton) {
				            	
			              }
			          })
			          .create();
				case POPUP_BIER_1:
					final ImageView img5 = new ImageView(this);
					img5.setImageResource(R.drawable.howtoplay_image4); //TODO
					 return new AlertDialog.Builder(PipelinesActivity.this)
			          .setTitle("How to play?! - 7")
		              .setView(img5)
			          .setMessage(res.getString(R.string.howtoplayPopUpBeer1))
			          .setPositiveButton("Oke", new DialogInterface.OnClickListener() {
			              public void onClick(DialogInterface dialog, int whichButton) {
				            	
			              }
			          })
			          .create();
		 }
		return null;
    }

	private void rotateImageView(int h, int w) {
		if(vibrate){
			Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
			v.vibrate(50);
		}
		 aantalstappen++;
         resetGeweest();
         setAllPipesEmpty();
		
         setValuesOfPipe(h,w);
         
         //ROTATE IMAGE 90 degrees
        Bitmap bmp = ((BitmapDrawable)grid[h][w].getDrawable()).getBitmap();
     	// Getting width & height of the given image.
     	int width = bmp.getWidth();
     	int height = bmp.getHeight();
     	// Setting post rotate to 90
     	Matrix mtx = new Matrix();
     	mtx.postRotate(90);
     	// Rotating Bitmap
     	Bitmap rotatedBMP = Bitmap.createBitmap(bmp, 0, 0, width, height, mtx, true);
     	BitmapDrawable bmd = new BitmapDrawable(rotatedBMP);
     	grid[h][w].setImageDrawable(bmd);
     	aantalgroen = 0;
     	aantalblauw = 0;
     	
     	for(int i=0; i< aantalbeginpuntengroen;i++){
     		checkFromStart(beginpuntengroen[i][0],beginpuntengroen[i][1],true, "groen");
     	}
       	for(int i=0; i< aantalbeginpuntenblauw;i++){
     		checkFromStart(beginpuntenblauw[i][0],beginpuntenblauw[i][1],true, "blauw");
     	}
	}
	
	
	private void setValuesOfPipe(int h, int w) {
		  //GROEN
     	//0 = niks // 1 = hoek // 2 = tstuk // 3 = begin //4 = eind//5 = streep//6=kruis
		//STREEP
		if ( type[h][w] == 5  )
        {
            if (gradenhoek[h][w] == 0)
            {
                gradenhoek[h][w] = 90;
                eigenuitgang[h][w][0] = 0;
                eigenuitgang[h][w][1] = 1;
                eigenuitgang[h][w][2] = 0;
                eigenuitgang[h][w][3] = 1;
            }
            else if (gradenhoek[h][w] == 90)
            {
           	 gradenhoek[h][w] = 180;
           	 eigenuitgang[h][w][0] = 1;
                eigenuitgang[h][w][1] = 0;
                eigenuitgang[h][w][2] = 1;
                eigenuitgang[h][w][3] = 0;
            }
            else if (gradenhoek[h][w] == 180)
            {
           	 gradenhoek[h][w] = 270;
           	 eigenuitgang[h][w][0] = 0;
                eigenuitgang[h][w][1] = 1;
                eigenuitgang[h][w][2] = 0;
                eigenuitgang[h][w][3] = 1;
            }
            else if(gradenhoek[h][w] == 270)
            {
           	 gradenhoek[h][w] = 0;
           	 eigenuitgang[h][w][0] = 1;
                eigenuitgang[h][w][1] = 0;
                eigenuitgang[h][w][2] = 1;
                eigenuitgang[h][w][3] = 0;
            }
        }
        //HOekjes
        else if ( type[h][w] == 1  )
        {
            if (gradenhoek[h][w] == 0)
            {
                gradenhoek[h][w] = 90;
                eigenuitgang[h][w][0] = 0;
                eigenuitgang[h][w][1] = 0;
                eigenuitgang[h][w][2] = 1;
                eigenuitgang[h][w][3] = 1;
            }
            else if (gradenhoek[h][w] == 90)
            {
           	 gradenhoek[h][w] = 180;
           	 eigenuitgang[h][w][0] = 1;
                eigenuitgang[h][w][1] = 0;
                eigenuitgang[h][w][2] = 0;
                eigenuitgang[h][w][3] = 1;
            }
            else if (gradenhoek[h][w] == 180)
            {
           	 gradenhoek[h][w] = 270;
           	 eigenuitgang[h][w][0] = 1;
                eigenuitgang[h][w][1] = 1;
                eigenuitgang[h][w][2] = 0;
                eigenuitgang[h][w][3] = 0;
            }
            else if(gradenhoek[h][w] == 270)
            {
           	 gradenhoek[h][w] = 0;
           	 eigenuitgang[h][w][0] = 0;
                eigenuitgang[h][w][1] = 1;
                eigenuitgang[h][w][2] = 1;
                eigenuitgang[h][w][3] = 0;
            }
        }
        //TSTUK
        else if ( type[h][w] == 2 )
        {
            if (gradenhoek[h][w] == 0)
            {
                gradenhoek[h][w] = 90;
                eigenuitgang[h][w][0] = 1;
                eigenuitgang[h][w][1] = 0;
                eigenuitgang[h][w][2] = 1;
                eigenuitgang[h][w][3] = 1;
            }
            else if (gradenhoek[h][w] == 90)
            {
           	 gradenhoek[h][w] = 180;
           	 eigenuitgang[h][w][0] = 1;
                eigenuitgang[h][w][1] = 1;
                eigenuitgang[h][w][2] = 0;
                eigenuitgang[h][w][3] = 1;
            }
            else if (gradenhoek[h][w] == 180)
            {
           	 gradenhoek[h][w] = 270;
           	 eigenuitgang[h][w][0] = 1;
                eigenuitgang[h][w][1] = 1;
                eigenuitgang[h][w][2] = 1;
                eigenuitgang[h][w][3] = 0;
            }
            else if(gradenhoek[h][w] == 270)
            {
           	 gradenhoek[h][w] = 0;
           	 eigenuitgang[h][w][0] = 0;
                eigenuitgang[h][w][1] = 1;
                eigenuitgang[h][w][2] = 1;
                eigenuitgang[h][w][3] = 1;
            }
        }
        //BEGIN
        else if ( type[h][w] == 3  )
        {
            if (gradenhoek[h][w] == 0)
            {
                gradenhoek[h][w] = 90;
                eigenuitgang[h][w][0] = 1;
                eigenuitgang[h][w][1] = 0;
                eigenuitgang[h][w][2] = 0;
                eigenuitgang[h][w][3] = 0;
            }
            else if (gradenhoek[h][w] == 90)
            {
           	 gradenhoek[h][w] = 180;
           	 eigenuitgang[h][w][0] = 0;
                eigenuitgang[h][w][1] = 1;
                eigenuitgang[h][w][2] = 0;
                eigenuitgang[h][w][3] = 0;
            }
            else if (gradenhoek[h][w] == 180)
            {
	           	 gradenhoek[h][w] = 270;
	           	 eigenuitgang[h][w][0] = 0;
                eigenuitgang[h][w][1] = 0;
                eigenuitgang[h][w][2] = 1;
                eigenuitgang[h][w][3] = 0;
            }
            else if(gradenhoek[h][w] == 270)
            {
	           	 gradenhoek[h][w] = 0;
	           	 eigenuitgang[h][w][0] = 0;
                eigenuitgang[h][w][1] = 0;
                eigenuitgang[h][w][2] = 0;
                eigenuitgang[h][w][3] = 1;
            }
        }
        //EIND
        else if ( type[h][w] == 4  )
        {
            if (gradenhoek[h][w] == 0)
            {
                gradenhoek[h][w] = 90;
                eigenuitgang[h][w][0] = 0;
                eigenuitgang[h][w][1] = 0;
                eigenuitgang[h][w][2] = 0;
                eigenuitgang[h][w][3] = 1;
            }
            else if (gradenhoek[h][w] == 90)
            {
	           	 gradenhoek[h][w] = 180;
	           	 eigenuitgang[h][w][0] = 1;
                eigenuitgang[h][w][1] = 0;
                eigenuitgang[h][w][2] = 0;
                eigenuitgang[h][w][3] = 0;
            }
            else if (gradenhoek[h][w] == 180)
            {
	           	 gradenhoek[h][w] = 270;
	           	 eigenuitgang[h][w][0] = 0;
                eigenuitgang[h][w][1] = 1;
                eigenuitgang[h][w][2] = 0;
                eigenuitgang[h][w][3] = 0;
            }
            else if(gradenhoek[h][w] == 270)
            {
	           	 gradenhoek[h][w] = 0;
	           	 eigenuitgang[h][w][0] = 0;
                eigenuitgang[h][w][1] = 0;
                eigenuitgang[h][w][2] = 1;
                eigenuitgang[h][w][3] = 0;
            }
        }
		//KRUIS
        else if ( type[h][w] == 6  )
        {
            if (gradenhoek[h][w] == 0)
            {
                gradenhoek[h][w] = 90;
                eigenuitgang[h][w][0] = 1;
                eigenuitgang[h][w][1] = 1;
                eigenuitgang[h][w][2] = 1;
                eigenuitgang[h][w][3] = 1;
            }
            else if (gradenhoek[h][w] == 90)
            {
	           	gradenhoek[h][w] = 180;
	           	eigenuitgang[h][w][0] = 1;
                eigenuitgang[h][w][1] = 1;
                eigenuitgang[h][w][2] = 1;
                eigenuitgang[h][w][3] = 1;
            }
            else if (gradenhoek[h][w] == 180)
            {
	            gradenhoek[h][w] = 270;
	           	eigenuitgang[h][w][0] = 1;
                eigenuitgang[h][w][1] = 1;
                eigenuitgang[h][w][2] = 1;
                eigenuitgang[h][w][3] = 1;
            }
            else if(gradenhoek[h][w] == 270)
            {
           	 	gradenhoek[h][w] = 0;
           	 	eigenuitgang[h][w][0] = 1;
                eigenuitgang[h][w][1] = 1;
                eigenuitgang[h][w][2] = 1;
                eigenuitgang[h][w][3] = 1;
            }
        }
		
       //BLAUW
     	//7=beginblauw//8=eindblauw//9=hoekblauw//10=streepblauw//11=tstukblauw//12=kruisblauw
        else if ( type[h][w] ==  10 )
        {
            if (gradenhoek[h][w] == 0)
            {
                gradenhoek[h][w] = 90;
                eigenuitgang[h][w][0] = 0;
                eigenuitgang[h][w][1] = 2;
                eigenuitgang[h][w][2] = 0;
                eigenuitgang[h][w][3] = 2;
            }
            else if (gradenhoek[h][w] == 90)
            {
           	 	gradenhoek[h][w] = 180;
           	 	eigenuitgang[h][w][0] = 2;
                eigenuitgang[h][w][1] = 0;
                eigenuitgang[h][w][2] = 2;
                eigenuitgang[h][w][3] = 0;
            }
            else if (gradenhoek[h][w] == 180)
            {
           	 	gradenhoek[h][w] = 270;
           	 	eigenuitgang[h][w][0] = 0;
                eigenuitgang[h][w][1] = 2;
                eigenuitgang[h][w][2] = 0;
                eigenuitgang[h][w][3] = 2;
            }
            else if(gradenhoek[h][w] == 270)
            {
           	 	gradenhoek[h][w] = 0;
           	 	eigenuitgang[h][w][0] = 2;
                eigenuitgang[h][w][1] = 0;
                eigenuitgang[h][w][2] = 2;
                eigenuitgang[h][w][3] = 0;
            }
        }
        //HOekjesblauw
        else if ( type[h][w] == 9  )
        {
            if (gradenhoek[h][w] == 0)
            {
                gradenhoek[h][w] = 90;
                eigenuitgang[h][w][0] = 0;
                eigenuitgang[h][w][1] = 0;
                eigenuitgang[h][w][2] = 2;
                eigenuitgang[h][w][3] = 2;
            }
            else if (gradenhoek[h][w] == 90)
            {
           	 	gradenhoek[h][w] = 180;
           	 	eigenuitgang[h][w][0] = 2;
                eigenuitgang[h][w][1] = 0;
                eigenuitgang[h][w][2] = 0;
                eigenuitgang[h][w][3] = 2;
            }
            else if (gradenhoek[h][w] == 180)
            {
           	 	gradenhoek[h][w] = 270;
           	 	eigenuitgang[h][w][0] = 2;
                eigenuitgang[h][w][1] = 2;
                eigenuitgang[h][w][2] = 0;
                eigenuitgang[h][w][3] = 0;
            }
            else if(gradenhoek[h][w] == 270)
            {
           	 	gradenhoek[h][w] = 0;
           	 	eigenuitgang[h][w][0] = 0;
                eigenuitgang[h][w][1] = 2;
                eigenuitgang[h][w][2] = 2;
                eigenuitgang[h][w][3] = 0;
            }
        }
        //TSTUKblauw
        else if ( type[h][w] == 11 )
        {
            if (gradenhoek[h][w] == 0)
            {
                gradenhoek[h][w] = 90;
                eigenuitgang[h][w][0] = 2;
                eigenuitgang[h][w][1] = 0;
                eigenuitgang[h][w][2] = 2;
                eigenuitgang[h][w][3] = 2;
            }
            else if (gradenhoek[h][w] == 90)
            {
           	 	gradenhoek[h][w] = 180;
           	 	eigenuitgang[h][w][0] = 2;
                eigenuitgang[h][w][1] = 2;
                eigenuitgang[h][w][2] = 0;
                eigenuitgang[h][w][3] = 2;
            }
            else if (gradenhoek[h][w] == 180)
            {
           	 	gradenhoek[h][w] = 270;
           	 	eigenuitgang[h][w][0] = 2;
                eigenuitgang[h][w][1] = 2;
                eigenuitgang[h][w][2] = 2;
                eigenuitgang[h][w][3] = 0;
            }
            else if(gradenhoek[h][w] == 270)
            {
           	 	gradenhoek[h][w] = 0;
           	 	eigenuitgang[h][w][0] = 0;
                eigenuitgang[h][w][1] = 2;
                eigenuitgang[h][w][2] = 2;
                eigenuitgang[h][w][3] = 2;
            }
        }
        //BEGINblauw
        else if ( type[h][w] == 7  )
        {
            if (gradenhoek[h][w] == 0)
            {
                gradenhoek[h][w] = 90;
                eigenuitgang[h][w][0] = 2;
                eigenuitgang[h][w][1] = 0;
                eigenuitgang[h][w][2] = 0;
                eigenuitgang[h][w][3] = 0;
            }
            else if (gradenhoek[h][w] == 90)
            {
           	 	gradenhoek[h][w] = 180;
           	 	eigenuitgang[h][w][0] = 0;
                eigenuitgang[h][w][1] = 2;
                eigenuitgang[h][w][2] = 0;
                eigenuitgang[h][w][3] = 0;
            }
            else if (gradenhoek[h][w] == 180)
            {
           	 	gradenhoek[h][w] = 270;
           	 	eigenuitgang[h][w][0] = 0;
                eigenuitgang[h][w][1] = 0;
                eigenuitgang[h][w][2] = 2;
                eigenuitgang[h][w][3] = 0;
            }
            else if(gradenhoek[h][w] == 270)
            {
           	 	gradenhoek[h][w] = 0;
           	 	eigenuitgang[h][w][0] = 0;
                eigenuitgang[h][w][1] = 0;
                eigenuitgang[h][w][2] = 0;
                eigenuitgang[h][w][3] = 2;
            }
        }
        //EINDblauw
        else if ( type[h][w] == 8  )
        {
            if (gradenhoek[h][w] == 0)
            {
                gradenhoek[h][w] = 90;
                eigenuitgang[h][w][0] = 0;
                eigenuitgang[h][w][1] = 0;
                eigenuitgang[h][w][2] = 0;
                eigenuitgang[h][w][3] = 2;
            }
            else if (gradenhoek[h][w] == 90)
            {
           	 	gradenhoek[h][w] = 180;
           	 	eigenuitgang[h][w][0] = 2;
                eigenuitgang[h][w][1] = 0;
                eigenuitgang[h][w][2] = 0;
                eigenuitgang[h][w][3] = 0;
            }
            else if (gradenhoek[h][w] == 180)
            {
	           	gradenhoek[h][w] = 270;
	           	eigenuitgang[h][w][0] = 0;
                eigenuitgang[h][w][1] = 2;
                eigenuitgang[h][w][2] = 0;
                eigenuitgang[h][w][3] = 0;
            }
            else if(gradenhoek[h][w] == 270)
            {
           	 	gradenhoek[h][w] = 0;
           	 	eigenuitgang[h][w][0] = 0;
                eigenuitgang[h][w][1] = 0;
                eigenuitgang[h][w][2] = 2;
                eigenuitgang[h][w][3] = 0;
            }
        }
        //KRUISblauw
        else if ( type[h][w] == 12  )
        {
            if (gradenhoek[h][w] == 0)
            {
                gradenhoek[h][w] = 90;
                eigenuitgang[h][w][0] = 2;
                eigenuitgang[h][w][1] = 2;
                eigenuitgang[h][w][2] = 2;
                eigenuitgang[h][w][3] = 2;
            }
            else if (gradenhoek[h][w] == 90)
            {
           	 	gradenhoek[h][w] = 180;
           	 	eigenuitgang[h][w][0] = 2;
                eigenuitgang[h][w][1] = 2;
                eigenuitgang[h][w][2] = 2;
                eigenuitgang[h][w][3] = 2;
            }
            else if (gradenhoek[h][w] == 180)
            {
	           	gradenhoek[h][w] = 270;
	           	eigenuitgang[h][w][0] = 2;
                eigenuitgang[h][w][1] = 2;
                eigenuitgang[h][w][2] = 2;
                eigenuitgang[h][w][3] = 2;
            }
            else if(gradenhoek[h][w] == 270)
            {
           	 	gradenhoek[h][w] = 0;
           	 	eigenuitgang[h][w][0] = 2;
                eigenuitgang[h][w][1] = 2;
                eigenuitgang[h][w][2] = 2;
                eigenuitgang[h][w][3] = 2;
            }
        }
        //KRUISCOMBI
        else if ( type[h][w] == 13  )
        {
            if (gradenhoek[h][w] == 0)
            {
                gradenhoek[h][w] = 90;
                eigenuitgang[h][w][0] = 1;
                eigenuitgang[h][w][1] = 2;
                eigenuitgang[h][w][2] = 1;
                eigenuitgang[h][w][3] = 2;
            }
            else if (gradenhoek[h][w] == 90)
            {
           	 	gradenhoek[h][w] = 180;
           	 	eigenuitgang[h][w][0] = 2;
                eigenuitgang[h][w][1] = 1;
                eigenuitgang[h][w][2] = 2;
                eigenuitgang[h][w][3] = 1;
            }
            else if (gradenhoek[h][w] == 180)
            {
           	 	gradenhoek[h][w] = 270;
           	 	eigenuitgang[h][w][0] = 1;
                eigenuitgang[h][w][1] = 2;
                eigenuitgang[h][w][2] = 1;
                eigenuitgang[h][w][3] = 2;
            }
            else if(gradenhoek[h][w] == 270)
            {
           	 	gradenhoek[h][w] = 0;
           	 	eigenuitgang[h][w][0] = 2;
                eigenuitgang[h][w][1] = 1;
                eigenuitgang[h][w][2] = 2;
                eigenuitgang[h][w][3] = 1;
            }
        }
        //HOEKJECOMBI
        else if ( type[h][w] == 14  )
        {
            if (gradenhoek[h][w] == 0)
            {
                gradenhoek[h][w] = 90;
                eigenuitgang[h][w][0] = 2;
                eigenuitgang[h][w][1] = 2;
                eigenuitgang[h][w][2] = 1;
                eigenuitgang[h][w][3] = 1;
            }
            else if (gradenhoek[h][w] == 90)
            {
           	 	gradenhoek[h][w] = 180;
           	 	eigenuitgang[h][w][0] = 1;
                eigenuitgang[h][w][1] = 2;
                eigenuitgang[h][w][2] = 2;
                eigenuitgang[h][w][3] = 1;
            }
            else if (gradenhoek[h][w] == 180)
            {
           	 	gradenhoek[h][w] = 270;
           	 	eigenuitgang[h][w][0] = 1;
                eigenuitgang[h][w][1] = 1;
                eigenuitgang[h][w][2] = 2;
                eigenuitgang[h][w][3] = 2;
            }
            else if(gradenhoek[h][w] == 270)
            {
           	 	gradenhoek[h][w] = 0;
           	 	eigenuitgang[h][w][0] = 2;
                eigenuitgang[h][w][1] = 1;
                eigenuitgang[h][w][2] = 1;
                eigenuitgang[h][w][3] = 2;
            }
        }
	}

	private void setAllPipesEmpty() {
		 for (int h = 0; h < HEIGHT; h++)
         {
             for (int w = 0; w < WIDTH; w++)
             {
            	//GROEN
        		//0 = niks // 1 = hoek // 2 = tstuk // 3 = begin //4 = eind//5 = streep//6=kruis
                 if ( type[h][w] == 5 ){
                   	 if(levelpack == 1){
            			 grid[h][w].setImageResource(R.drawable.streep);
            		 }else if(levelpack == 2){
            			 grid[h][w].setImageResource(R.drawable.alien1streepleeg);
            		 }else if(levelpack == 3){
            			 grid[h][w].setImageResource(R.drawable.bierstreep2);
            		 }
                     rotateGreenPipe(h, w, gradenhoek[h][w]);
                 }
                 else if ( type[h][w] == 1 ){
                   	 if(levelpack == 1){
            			 grid[h][w].setImageResource(R.drawable.hoekje);
            		 }else if(levelpack == 2){
            			 grid[h][w].setImageResource(R.drawable.alien1hoekjeleeg);
            		 }else if(levelpack == 3){
            			 grid[h][w].setImageResource(R.drawable.bierhoekje2);
            		 }
                	 rotateGreenPipe(h, w, gradenhoek[h][w]);
                	 }
                 else if (type[h][w] == 2 ){
                   	 if(levelpack == 1){
            			 grid[h][w].setImageResource(R.drawable.tstukje);
            		 }else if(levelpack == 2){
            			 grid[h][w].setImageResource(R.drawable.alien1tstukleeg);
            		 }else if(levelpack == 3){
            			 grid[h][w].setImageResource(R.drawable.biertstukje2);
            		 }
                	 rotateGreenPipe(h, w, gradenhoek[h][w]);
                 }
                 else if ( type[h][w] == 4 ){
                   	 if(levelpack == 1){
            			 grid[h][w].setImageResource(R.drawable.eindpunt);
            		 }else if(levelpack == 2){
            			 grid[h][w].setImageResource(R.drawable.alien1eindleeg);
            		 }else if(levelpack == 3){
            			 grid[h][w].setImageResource(R.drawable.biereindpunt);
            		 }
                	 rotateGreenPipe(h, w, gradenhoek[h][w]);
                 }
                 else if( type[h][w] == 6){
                   	 if(levelpack == 1){
            			 grid[h][w].setImageResource(R.drawable.kruis);
            		 }else if(levelpack == 2){
            			 grid[h][w].setImageResource(R.drawable.alien1kruisleeg);
            		 }else if(levelpack == 3){
            			 grid[h][w].setImageResource(R.drawable.bierkruis2);
            		 }
                	 rotateGreenPipe(h, w, gradenhoek[h][w]);
                 }
               //BLAUW
         		//7=beginblauw//8=eindblauw//9=hoekblauw//10=streepblauw//11=tstukblauw//12=kruisblauw
                 else if ( type[h][w] == 8 ){
                		if(levelpack == 1){
            				grid[h][w].setImageResource(R.drawable.eindpunt2);
            			}else if(levelpack == 2){
            				grid[h][w].setImageResource(R.drawable.alien2eindleeg);
            			}
                     rotateGreenPipe(h, w, gradenhoek[h][w]);
                 }
                 else if ( type[h][w] == 9 ){
                	 	if(levelpack == 1){
            				grid[h][w].setImageResource(R.drawable.hoekje2);
            			}else if(levelpack == 2){
            				grid[h][w].setImageResource(R.drawable.alien2hoekjeleeg);
            			}
                	 rotateGreenPipe(h, w, gradenhoek[h][w]);
                	 }
                 else if (type[h][w] == 10 ){
                	 	if(levelpack == 1){
            				grid[h][w].setImageResource(R.drawable.streep2);
            			}else if(levelpack == 2){
            				grid[h][w].setImageResource(R.drawable.alien2streepleeg);
            			}
                	 rotateGreenPipe(h, w, gradenhoek[h][w]);
                 }
                 else if ( type[h][w] == 11 ){
                	 	if(levelpack == 1){
            				grid[h][w].setImageResource(R.drawable.tstukje2);
            			}else if(levelpack == 2){
            				grid[h][w].setImageResource(R.drawable.alien2tstukleeg);
            			}
                	 rotateGreenPipe(h, w, gradenhoek[h][w]);
                 }
                 else if( type[h][w] == 12){
                	 	if(levelpack == 1){
            				grid[h][w].setImageResource(R.drawable.kruis2);
            			}else if(levelpack == 2){
            				grid[h][w].setImageResource(R.drawable.alien2kruisleeg);
            			}
                	 rotateGreenPipe(h, w, gradenhoek[h][w]);
                 }
                 //kruiscombi
                 else if( type[h][w] == 13){
                	 	if(levelpack == 1){
            				 grid[h][w].setImageResource(R.drawable.kruiscombi);
            			}else if(levelpack == 2){
            				grid[h][w].setImageResource(R.drawable.alienkruiscombileeg);
            			}
                	 rotateGreenPipe(h, w, gradenhoek[h][w]);
                 }
                 //hoekjecombi
                 else if( type[h][w] == 14){
                	 	if(levelpack == 1){
            				grid[h][w].setImageResource(R.drawable.hoekjecombi);
            			}else if(levelpack == 2){
            				grid[h][w].setImageResource(R.drawable.alienhoekjecombileeg);
            			}
                	 rotateGreenPipe(h, w, gradenhoek[h][w]);
                 }
             }
         }
		
	}
	private void setUpViews() {
		Display display = getWindowManager().getDefaultDisplay(); 
		int screenwidth = display.getWidth()/8;
		
		int screenheightOver = (display.getHeight()-(10*screenwidth))-10;
		
		if(screenheightOver > 30){
			 // Lookup R.layout.main
	        LinearLayout layout = (LinearLayout)findViewById(R.id.layoutAdsLevel);
	        // Create the adView
	        // Please replace MY_BANNER_UNIT_ID with your AdMob Publisher ID
	        adView1 = new AdView(this, AdSize.BANNER, MY_BANNER_UNIT_ID);
	        // Add the adView to it
	        layout.addView(adView1);
	        // Initiate a generic request to load it with an ad
	        AdRequest request = new AdRequest();
	        //request.setTesting(true);
	        adView1.loadAd(request);     
		}
		else{
			RelativeLayout content = (RelativeLayout)findViewById(R.id.content);
			RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT);
			lp.setMargins(0, 0, 0, 0);
			content.setLayoutParams(lp);
		}
		
		RelativeLayout layoutBackground = (RelativeLayout)findViewById(R.id.layoutBackground);
		
		if(levelpack == 1){
			layoutBackground.setBackgroundResource(R.drawable.western_bg);
		}else if(levelpack == 2){
			layoutBackground.setBackgroundResource(R.drawable.alienbackground);
		}
		else if(levelpack == 3){
			layoutBackground.setBackgroundResource(R.drawable.bierachtergrond);
		}
		
		//background grid
		if(backgroundgrid){
			LinearLayout layoutGrid = (LinearLayout)findViewById(R.id.gridLayout);
			if(levelpack == 1){
				layoutGrid.setBackgroundResource(R.drawable.grid_rooster);
			}else if(levelpack == 2){
				layoutGrid.setBackgroundResource(R.drawable.aliengrid_rooster);
			}else if(levelpack == 3){
				layoutGrid.setBackgroundResource(R.drawable.aliengrid_rooster);
			}
		}
		
		grid[0][0] = 	(ImageView)findViewById(R.id.ImageView00);
		grid[0][1] =	(ImageView)findViewById(R.id.ImageView01);
		grid[0][2] =	(ImageView)findViewById(R.id.ImageView02);
		grid[0][3] =	(ImageView)findViewById(R.id.ImageView03);
		grid[0][4] = 	(ImageView)findViewById(R.id.ImageView04);
		grid[0][5] =	(ImageView)findViewById(R.id.ImageView05);
		grid[0][6] =	(ImageView)findViewById(R.id.ImageView06);
		grid[0][7] =	(ImageView)findViewById(R.id.ImageView07);
		grid[0][8] =	(ImageView)findViewById(R.id.ImageView08);
		grid[0][9] =	(ImageView)findViewById(R.id.ImageView09);
		
		grid[1][0] =	(ImageView)findViewById(R.id.ImageView10);	
		grid[1][1] =	(ImageView)findViewById(R.id.ImageView11);  
		grid[1][2] =	(ImageView)findViewById(R.id.ImageView12);	
		grid[1][3] =	(ImageView)findViewById(R.id.ImageView13);	
		grid[1][4] =	(ImageView)findViewById(R.id.ImageView14);	
		grid[1][5] =	(ImageView)findViewById(R.id.ImageView15);  
		grid[1][6] =	(ImageView)findViewById(R.id.ImageView16);	
		grid[1][7] =	(ImageView)findViewById(R.id.ImageView17);
		grid[1][8] =	(ImageView)findViewById(R.id.ImageView18);	
		grid[1][9] =	(ImageView)findViewById(R.id.ImageView19);
		
		grid[2][0] =	(ImageView)findViewById(R.id.ImageView20);	
		grid[2][1] =	(ImageView)findViewById(R.id.ImageView21);	
		grid[2][2] =	(ImageView)findViewById(R.id.ImageView22);	
		grid[2][3] =	(ImageView)findViewById(R.id.ImageView23);	
		grid[2][4] =	(ImageView)findViewById(R.id.ImageView24);	
		grid[2][5] =	(ImageView)findViewById(R.id.ImageView25);	
		grid[2][6] =	(ImageView)findViewById(R.id.ImageView26);	
		grid[2][7] =	(ImageView)findViewById(R.id.ImageView27);	
		grid[2][8] =	(ImageView)findViewById(R.id.ImageView28);	
		grid[2][9] =	(ImageView)findViewById(R.id.ImageView29);
		
		grid[3][0] =	(ImageView)findViewById(R.id.ImageView30);	
		grid[3][1] =	(ImageView)findViewById(R.id.ImageView31);	
		grid[3][2] =	(ImageView)findViewById(R.id.ImageView32);	
		grid[3][3] =	(ImageView)findViewById(R.id.ImageView33);	
		grid[3][4] =	(ImageView)findViewById(R.id.ImageView34);	
		grid[3][5] =	(ImageView)findViewById(R.id.ImageView35);	
		grid[3][6] =	(ImageView)findViewById(R.id.ImageView36);	
		grid[3][7] =	(ImageView)findViewById(R.id.ImageView37);	
		grid[3][8] =	(ImageView)findViewById(R.id.ImageView38);	
		grid[3][9] =	(ImageView)findViewById(R.id.ImageView39);	
		
		grid[4][0] =	(ImageView)findViewById(R.id.ImageView40);	
		grid[4][1] =	(ImageView)findViewById(R.id.ImageView41);	
		grid[4][2] =	(ImageView)findViewById(R.id.ImageView42);	
		grid[4][3] =	(ImageView)findViewById(R.id.ImageView43);	
		grid[4][4] =	(ImageView)findViewById(R.id.ImageView44);	
		grid[4][5] =	(ImageView)findViewById(R.id.ImageView45);	
		grid[4][6] =	(ImageView)findViewById(R.id.ImageView46);	
		grid[4][7] =	(ImageView)findViewById(R.id.ImageView47);	
		grid[4][8] =	(ImageView)findViewById(R.id.ImageView48);	
		grid[4][9] =	(ImageView)findViewById(R.id.ImageView49);
		
		grid[5][0] =	(ImageView)findViewById(R.id.ImageView50);	
		grid[5][1] =	(ImageView)findViewById(R.id.ImageView51);	
		grid[5][2] =	(ImageView)findViewById(R.id.ImageView52);	
		grid[5][3] =	(ImageView)findViewById(R.id.ImageView53);	
		grid[5][4] =	(ImageView)findViewById(R.id.ImageView54);	
		grid[5][5] =	(ImageView)findViewById(R.id.ImageView55);	
		grid[5][6] =	(ImageView)findViewById(R.id.ImageView56);	
		grid[5][7] =	(ImageView)findViewById(R.id.ImageView57);
		grid[5][8] =	(ImageView)findViewById(R.id.ImageView58);	
		grid[5][9] =	(ImageView)findViewById(R.id.ImageView59);
		
		grid[6][0] =	(ImageView)findViewById(R.id.ImageView60);	
		grid[6][1] =	(ImageView)findViewById(R.id.ImageView61);	
		grid[6][2] =	(ImageView)findViewById(R.id.ImageView62);	
		grid[6][3] =	(ImageView)findViewById(R.id.ImageView63);	
		grid[6][4] =	(ImageView)findViewById(R.id.ImageView64);	
		grid[6][5] =	(ImageView)findViewById(R.id.ImageView65);	
		grid[6][6] =	(ImageView)findViewById(R.id.ImageView66);	
		grid[6][7] =	(ImageView)findViewById(R.id.ImageView67);
		grid[6][8] =	(ImageView)findViewById(R.id.ImageView68);	
		grid[6][9] =	(ImageView)findViewById(R.id.ImageView69);
		
		grid[7][0] =	(ImageView)findViewById(R.id.ImageView70);	
		grid[7][1] =	(ImageView)findViewById(R.id.ImageView71);	
		grid[7][2] =	(ImageView)findViewById(R.id.ImageView72);	
		grid[7][3] =	(ImageView)findViewById(R.id.ImageView73);	
		grid[7][4] =	(ImageView)findViewById(R.id.ImageView74);	
		grid[7][5] =	(ImageView)findViewById(R.id.ImageView75);	
		grid[7][6] =	(ImageView)findViewById(R.id.ImageView76);	
		grid[7][7] =	(ImageView)findViewById(R.id.ImageView77);
		grid[7][8] =	(ImageView)findViewById(R.id.ImageView78);	
		grid[7][9] =	(ImageView)findViewById(R.id.ImageView79);
		
		grid[8][0] =	(ImageView)findViewById(R.id.ImageView80);	
		grid[8][1] =	(ImageView)findViewById(R.id.ImageView81);	
		grid[8][2] =	(ImageView)findViewById(R.id.ImageView82);	
		grid[8][3] =	(ImageView)findViewById(R.id.ImageView83);	
		grid[8][4] =	(ImageView)findViewById(R.id.ImageView84);	
		grid[8][5] =	(ImageView)findViewById(R.id.ImageView85);	
		grid[8][6] =	(ImageView)findViewById(R.id.ImageView86);	
		grid[8][7] =	(ImageView)findViewById(R.id.ImageView87);
		grid[8][8] =	(ImageView)findViewById(R.id.ImageView88);	
		grid[8][9] =	(ImageView)findViewById(R.id.ImageView89);
		
		grid[9][0] =	(ImageView)findViewById(R.id.ImageView90);	
		grid[9][1] =	(ImageView)findViewById(R.id.ImageView91);	
		grid[9][2] =	(ImageView)findViewById(R.id.ImageView92);	
		grid[9][3] =	(ImageView)findViewById(R.id.ImageView93);	
		grid[9][4] =	(ImageView)findViewById(R.id.ImageView94);	
		grid[9][5] =	(ImageView)findViewById(R.id.ImageView95);	
		grid[9][6] =	(ImageView)findViewById(R.id.ImageView96);	
		grid[9][7] =	(ImageView)findViewById(R.id.ImageView97);
		grid[9][8] =	(ImageView)findViewById(R.id.ImageView98);	
		grid[9][9] =	(ImageView)findViewById(R.id.ImageView99);
		
		grid[10][0] =	(ImageView)findViewById(R.id.ImageView100);	
		grid[10][1] =	(ImageView)findViewById(R.id.ImageView101);	
		grid[10][2] =	(ImageView)findViewById(R.id.ImageView102);	
		grid[10][3] =	(ImageView)findViewById(R.id.ImageView103);	
		grid[10][4] =	(ImageView)findViewById(R.id.ImageView104);	
		grid[10][5] =	(ImageView)findViewById(R.id.ImageView105);	
		grid[10][6] =	(ImageView)findViewById(R.id.ImageView106);	
		grid[10][7] =	(ImageView)findViewById(R.id.ImageView107);
		grid[10][8] =	(ImageView)findViewById(R.id.ImageView108);	
		grid[10][9] =	(ImageView)findViewById(R.id.ImageView109);
		
		grid[11][0] =	(ImageView)findViewById(R.id.ImageView110);	
		grid[11][1] =	(ImageView)findViewById(R.id.ImageView111);	
		grid[11][2] =	(ImageView)findViewById(R.id.ImageView112);	
		grid[11][3] =	(ImageView)findViewById(R.id.ImageView113);	
		grid[11][4] =	(ImageView)findViewById(R.id.ImageView114);	
		grid[11][5] =	(ImageView)findViewById(R.id.ImageView115);	
		grid[11][6] =	(ImageView)findViewById(R.id.ImageView116);	
		grid[11][7] =	(ImageView)findViewById(R.id.ImageView117);
		grid[11][8] =	(ImageView)findViewById(R.id.ImageView118);	
		grid[11][9] =	(ImageView)findViewById(R.id.ImageView119);
		
		for (int h = 0; h < HEIGHT; h++)
        {
            for (int w = 0; w < WIDTH; w++)
            {
            	if(h==0 || h == (HEIGHT-1))
            	{
            		grid[h][w].getLayoutParams().height = 0;
    				grid[h][w].getLayoutParams().width = screenwidth;
            	}
            	else if(w==0 || w == (WIDTH-1))
            	{
            		grid[h][w].getLayoutParams().height = screenwidth;
    				grid[h][w].getLayoutParams().width = 0;
            	}
            	else{
            		grid[h][w].getLayoutParams().height = screenwidth;
    				grid[h][w].getLayoutParams().width = screenwidth;
            	}
            }
        }
	}
	private void setClickListeners() {
		//rij 0 en 11 geen 
		//kolommen 0 en 9 geen
		
		grid[1][1].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {rotateImageView(1,1);}});
		grid[1][2].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {rotateImageView(1,2);}});
		grid[1][3].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {rotateImageView(1,3);}});
		grid[1][4].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {rotateImageView(1,4);}});
		grid[1][5].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {rotateImageView(1,5);}});
		grid[1][6].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {rotateImageView(1,6);}});
		grid[1][7].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {rotateImageView(1,7);}});
		grid[1][8].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {rotateImageView(1,8);}});
		
		grid[2][1].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {rotateImageView(2,1);}});
		grid[2][2].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {rotateImageView(2,2);}});
		grid[2][3].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {rotateImageView(2,3);}});
		grid[2][4].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {rotateImageView(2,4);}});
		grid[2][5].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {rotateImageView(2,5);}});
		grid[2][6].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {rotateImageView(2,6);}});
		grid[2][7].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {rotateImageView(2,7);}});
		grid[2][8].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {rotateImageView(2,8);}});
		
		grid[3][1].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {rotateImageView(3,1);}});
		grid[3][2].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {rotateImageView(3,2);}});
		grid[3][3].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {rotateImageView(3,3);}});
		grid[3][4].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {rotateImageView(3,4);}});
		grid[3][5].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {rotateImageView(3,5);}});
		grid[3][6].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {rotateImageView(3,6);}});
		grid[3][7].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {rotateImageView(3,7);}});
		grid[3][8].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {rotateImageView(3,8);}});
		
		grid[4][1].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {rotateImageView(4,1);}});
		grid[4][2].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {rotateImageView(4,2);}});
		grid[4][3].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {rotateImageView(4,3);}});
		grid[4][4].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {rotateImageView(4,4);}});
		grid[4][5].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {rotateImageView(4,5);}});
		grid[4][6].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {rotateImageView(4,6);}});
		grid[4][7].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {rotateImageView(4,7);}});
		grid[4][8].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {rotateImageView(4,8);}});
		
		grid[5][1].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {rotateImageView(5,1);}});
		grid[5][2].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {rotateImageView(5,2);}});
		grid[5][3].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {rotateImageView(5,3);}});
		grid[5][4].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {rotateImageView(5,4);}});
		grid[5][5].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {rotateImageView(5,5);}});
		grid[5][6].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {rotateImageView(5,6);}});
		grid[5][7].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {rotateImageView(5,7);}});
		grid[5][8].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {rotateImageView(5,8);}});
		
		grid[6][1].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {rotateImageView(6,1);}});
		grid[6][2].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {rotateImageView(6,2);}});
		grid[6][3].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {rotateImageView(6,3);}});
		grid[6][4].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {rotateImageView(6,4);}});
		grid[6][5].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {rotateImageView(6,5);}});
		grid[6][6].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {rotateImageView(6,6);}});
		grid[6][7].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {rotateImageView(6,7);}});
		grid[6][8].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {rotateImageView(6,8);}});
		
		grid[7][1].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {rotateImageView(7,1);}});
		grid[7][2].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {rotateImageView(7,2);}});
		grid[7][3].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {rotateImageView(7,3);}});
		grid[7][4].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {rotateImageView(7,4);}});
		grid[7][5].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {rotateImageView(7,5);}});
		grid[7][6].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {rotateImageView(7,6);}});
		grid[7][7].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {rotateImageView(7,7);}});
		grid[7][8].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {rotateImageView(7,8);}});
		
		grid[8][1].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {rotateImageView(8,1);}});
		grid[8][2].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {rotateImageView(8,2);}});
		grid[8][3].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {rotateImageView(8,3);}});
		grid[8][4].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {rotateImageView(8,4);}});
		grid[8][5].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {rotateImageView(8,5);}});
		grid[8][6].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {rotateImageView(8,6);}});
		grid[8][7].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {rotateImageView(8,7);}});
		grid[8][8].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {rotateImageView(8,8);}});
		
		grid[9][1].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {rotateImageView(9,1);}});
		grid[9][2].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {rotateImageView(9,2);}});
		grid[9][3].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {rotateImageView(9,3);}});
		grid[9][4].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {rotateImageView(9,4);}});
		grid[9][5].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {rotateImageView(9,5);}});
		grid[9][6].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {rotateImageView(9,6);}});
		grid[9][7].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {rotateImageView(9,7);}});
		grid[9][8].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {rotateImageView(9,8);}});
		
		grid[10][1].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {rotateImageView(10,1);}});
		grid[10][2].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {rotateImageView(10,2);}});
		grid[10][3].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {rotateImageView(10,3);}});
		grid[10][4].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {rotateImageView(10,4);}});
		grid[10][5].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {rotateImageView(10,5);}});
		grid[10][6].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {rotateImageView(10,6);}});
		grid[10][7].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {rotateImageView(10,7);}});
		grid[10][8].setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) {rotateImageView(10,8);}});
		
	}
}